package org.simple.utils;

import cn.hutool.core.util.ReflectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.simple.annocation.Query;

import java.lang.reflect.Field;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author jihu
 * date 2022-09-21
 */
public class QueryWrapperUtils<T> {


    public static final String SIGN = "_";

    private static Pattern humpPattern = Pattern.compile("[A-Z]");

    public static <T> QueryWrapper<T> initQueryWrapper(Object filter){
        QueryWrapper<T> queryWrapper = new QueryWrapper<T>();
        if(Objects.nonNull(filter)){
            Field[] fields = ReflectUtil.getFields(filter.getClass());
            for (Field field : fields) {
                Query query = field.getAnnotation(Query.class);
                if(Objects.nonNull(query)){
                    Object value = ReflectUtil.getFieldValue(filter, field.getName());
                    if(Objects.nonNull(value) && StringUtils.isNotBlank(value.toString())){
                        switch (query.type()){
                            case EQ:
                                queryWrapper.eq(humpToLine(field.getName()),value);
                                break;
                            case GT:
                                queryWrapper.gt(humpToLine(field.getName()),value);
                                break;
                            case GE:
                                queryWrapper.ge(humpToLine(field.getName()),value);
                                break;
                            case LT:
                                queryWrapper.lt(humpToLine(field.getName()),value);
                                break;
                            case LE:
                                queryWrapper.le(humpToLine(field.getName()),value);
                                break;
                            case LIKE:
                                queryWrapper.like(humpToLine(field.getName()),value);
                                break;
                            case LIKE_RIGHT:
                                queryWrapper.likeRight(humpToLine(field.getName()),value);
                                break;
                            case LIKE_LEFT:
                                queryWrapper.likeLeft(humpToLine(field.getName()),value);
                                break;
                        }
                    }
                }
            }

        }
        return queryWrapper;
    }

    /**
     *  驼峰转下划线
     * @param str
     * @return
     */
    public static String humpToLine(String str) {
        Matcher matcher = humpPattern.matcher(str);
        StringBuffer sb = new StringBuffer();
        while (matcher.find()) {
            matcher.appendReplacement(sb, SIGN + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }




}
